// pages/topay/topay.js

import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    touxiang: "",
    nicheng: "",
    orderid:"",
    uqid:"",
    total:"",
    total1: "",
    alltime:"",
    couponid:"",
    showcoupon:false,
    canusecoupon:"",
    youhuijia:"",
   
  },

  showcoupon:function(){
    this.setData({
      showcoupon: !this.data.showcoupon
    })
  },

 


  userSubmit: function (e) {
    if (this.data.uqid) {
      console.log(e.detail.formId);
      ajax('/api/user/form_id', {
        data: {
          uqid: this.data.uqid,
          form_id: e.detail.formId
        },
        header: {
          'Accept': 'application/json', // 默认值
        },
        method: 'post'
      }, res => { })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderid=options.orderid
    var uqid = wx.getStorageSync("uqid");
    this.setData({
      orderid: orderid,
      uqid: uqid
    })
    console.log(orderid)
    this.getInfo()
    this.getguiziinfo()
  },


  topaysuccess:function(){
    var that=this
    ajax("/api/scan/please", {
      data: {
        uqid: this.data.uqid,
        order_id: this.data.orderid,
        coupons_id: this.data.couponid
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'post'
    }, res => {
      console.log(res);
      if (res.error == 0) {
        var appId = res.data.sin_data.appId;
        var timeStamp = res.data.sin_data.timeStamp;
        var nonceStr = res.data.sin_data.nonceStr;
        var prepay_id = res.data.sin_data.package;
        var paySign = res.data.sin_data.paySign;
        var signType = res.data.sin_data.signType;

        wx.requestPayment({
          timeStamp: timeStamp,
          nonceStr: nonceStr,
          package: prepay_id,
          signType: signType,
          paySign: paySign,
          success(res) {
            console.log(res)
            wx.redirectTo({
              url: '/pages/paysuccess/paysuccess?orderid='+that.data.orderid,
            })
          },
          fail(res) {

          }
        })
      }
      if (res.error == 4034){

        console.log(res)

        wx.redirectTo({
          url: '/pages/paysuccess/paysuccess?orderid=' + res.data.order_id,
        })
      }
    })

    // wx.navigateTo({
    //   url: '/pages/paysuccess/paysuccess?orderid='+this.data.orderid,
    // })
  },

  getInfo: function () {
    var uqid = wx.getStorageSync("uqid");
    ajax("/api/user/user_info", {
      data: {
        uqid: uqid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
    }, res => {
      console.log(res);
      if (res.error == 0) {
        console.log("set 1 次")
        this.setData({
          touxiang: res.data.avatar,
          nicheng: res.data.nickname
        });

      }
    })
  },
  clickdanxuan:function(e){
    var index = e.currentTarget.dataset.index
    var str = "canusecoupon[" + index +"].isselect"

    if (this.data.canusecoupon[index].isselect) {
      this.setData({
        [str]: false
      })
      this.setData({
        couponid:"",
        youhuijia: 0,
        total1: this.data.total 
      })

      
    }else{
      for (var x in this.data.canusecoupon) {
        var str1 = "canusecoupon[" + x + "].isselect"
        this.setData({
          [str1]: false
        })
      }
      this.setData({
        [str]: true
      })
      this.setData({
        couponid: this.data.canusecoupon[index].id,
        youhuijia: this.data.canusecoupon[index].price,
        total1: this.data.total - this.data.canusecoupon[index].price
      })
      
    }

    

    
  },

  getguiziinfo: function () {
    ajax("/api/scan/info", {
      data: {
        uqid: this.data.uqid,
        order_id: this.data.orderid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'GET'
    }, res => {
      console.log(res);
      if (res.error == 0) {//获取进展 pid

        for (var x in res.data.coupons){
          res.data.coupons[x].isselect=false
        }
       this.setData({
        
         total:res.data.total,
         total1: res.data.total,
         alltime: res.data.use_at ,
         canusecoupon: res.data.coupons
       })

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})